//
//  SLWaterFlowLayout.m
//  SLKit
//
//  Created by S_LINE on 2021/12/29.
//  Copyright © 2021 S_LINE. All rights reserved.
//

#import "SLWaterFlowLayout.h"

#import "NSObject+SLSafe.h"

@interface SLWaterFlowLayout()

/// 每一列高度
@property (nonatomic, strong) NSMutableArray<NSNumber *> *columnHeights;

/// 所有的 attribute 数组
@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *attributesArray;

@end

@implementation SLWaterFlowLayout

- (void)prepareLayout {
    [super prepareLayout];
    
    /// 初始化数据
    self.columnHeights = [[NSMutableArray alloc] init];
    for (int column = 0; column < self.column; column++) {
        self.columnHeights[column] = @(0);
    }
    
    self.attributesArray = [[NSMutableArray alloc] init];
    NSInteger numberOfSections = [self.collectionView numberOfSections];
    for (int section = 0; section < numberOfSections; section++) {
        NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:section];
        for (int itemIndex = 0; itemIndex < numberOfItems; itemIndex++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:itemIndex inSection:section];
            
            UICollectionViewLayoutAttributes *attribute = [self layoutAttributesForItemAtIndexPath:indexPath];
            [self.attributesArray addObject:attribute];
        }
    }
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    // 获取item需要的高度
    CGFloat itemHeight = [self.delegate collectionViewLayout:self heightForItemAtIndexPath:indexPath];
    // 获取总高度最小的列
    NSInteger minHeightIndex = [self getMinHeightColumnIndex];
    // 最小的高度
    CGFloat minHeight = [self.columnHeights[minHeightIndex] doubleValue];
    // 计算item的frame
    CGFloat x = self.contentInsets.left + (self.itemWidth + self.columnSpacing) * minHeightIndex;
    CGFloat y = minHeight == 0 ? self.contentInsets.top : (minHeight + self.lineSpacing);
    layoutAttributes.frame = CGRectMake(x, y, self.itemWidth, itemHeight);
    // 更新最小高度列的当前高度
    self.columnHeights[minHeightIndex] = @(CGRectGetMaxY(layoutAttributes.frame));
    return layoutAttributes;
}

/// 获取所有列中高度最小的列的index
- (NSInteger)getMinHeightColumnIndex {
    __block NSInteger index = 0;
    [self.columnHeights enumerateObjectsUsingBlock:^(NSNumber * _Nonnull columnHeight, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([columnHeight doubleValue] < [self.columnHeights[index] doubleValue]) {
            index = idx;
        }
    }];
    return index;
}

- (CGSize)collectionViewContentSize {
    NSInteger mostColumn = [self getMaxHeightColumnIndex];
    //所有列当中最大的高度
    CGFloat mostHeight = [[self.columnHeights sl_objectAtIndex:mostColumn] floatValue];
    return CGSizeMake(CGRectGetWidth(self.collectionView.bounds), mostHeight + self.contentInsets.top);
}

/// 获取所有列中高度最大的列的index
- (NSInteger)getMaxHeightColumnIndex {
    __block NSInteger index = 0;
    [self.columnHeights enumerateObjectsUsingBlock:^(NSNumber * _Nonnull columnHeight, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([columnHeight doubleValue] > [self.columnHeights[index] doubleValue]) {
            index = idx;
        }
    }];
    return index;
}

- (NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return self.attributesArray;
}

@end
